
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using FibMatrix.Framework;

[CustomEditor(typeof(UGUIButtonEx))]
public class UGUIButtonExEditor:UnityEditor.UI.ButtonEditor
{
    private SerializedProperty pressScaleProp, normalScaleProp;
    private SerializedProperty pressColorProp, normalColorProp;
    private SerializedProperty pressSpriteProp;
    private SerializedProperty clickIntervalProp;
    
    private SerializedProperty transitionProp;
    private SerializedProperty disabledSpriteProp;

    private SerializedProperty animationProp;
    private SerializedProperty animateClipNameProp;
    private SerializedProperty animPrecentProp;

    private int chooseIndex = 0;
    private float animPrecent = 1.0f;
    
    protected override void OnEnable()
    {
        base.OnEnable();
        pressScaleProp = serializedObject.FindProperty( "pressScale" );
        normalScaleProp = serializedObject.FindProperty( "normalScale" );
        pressColorProp = serializedObject.FindProperty( "pressColor" );
        normalColorProp = serializedObject.FindProperty( "normalColor" );
        clickIntervalProp = serializedObject.FindProperty( "clickInterval" );
        pressSpriteProp = serializedObject.FindProperty("pressSprite");
        
        transitionProp = serializedObject.FindProperty("m_Transition");
        disabledSpriteProp = serializedObject.FindProperty("m_SpriteState.m_DisabledSprite");

        animationProp = serializedObject.FindProperty("animation");
        animateClipNameProp = serializedObject.FindProperty("animateClipName");
        animPrecentProp = serializedObject.FindProperty("animPrecent");
    }

    public override void OnInspectorGUI()
    {
        // base.OnInspectorGUI();
        UGUIButtonEx btn = (UGUIButtonEx)target;
        btn.scaleTweemTarget = EditorGUILayout.ObjectField("Scale Tween Target",btn.scaleTweemTarget, typeof(Transform), true) as Transform;
        
        if (btn.scaleTweemTarget != null)
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(pressScaleProp);
            EditorGUILayout.PropertyField(normalScaleProp);
            EditorGUI.indentLevel--;
        }

        
        EditorGUILayout.PropertyField(clickIntervalProp);
        btn.animMode = (ButtonAnimMode) EditorGUILayout.EnumPopup("AnimMode:", btn.animMode);

        if (btn.animMode == ButtonAnimMode.Color)
        {
            btn.colorTweenTarget = EditorGUILayout.ObjectField("Color Tween Target",btn.colorTweenTarget, typeof(Graphic), true) as Graphic;
            if (btn.colorTweenTarget != null)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(pressColorProp);
                EditorGUILayout.PropertyField(normalColorProp);
                EditorGUI.indentLevel--;
            }

        }else if (btn.animMode == ButtonAnimMode.SpriteSwap)
        {
            btn.colorTweenTarget = EditorGUILayout.ObjectField("Target",btn.colorTweenTarget, typeof(Graphic), true) as Graphic;
            if (btn.colorTweenTarget != null)
            {            
                EditorGUILayout.PropertyField(pressSpriteProp);
            }
        }

        EditorGUILayout.PropertyField(transitionProp);
        EditorGUILayout.PropertyField(disabledSpriteProp);
        
        
        if (btn.animMode == ButtonAnimMode.Animation)
        {
            if (btn.GetComponent<Animator>() == null)
            {
                // 生成按钮
                if (GUILayout.Button("生成Animator"))
                {
                    btn.animation = btn.gameObject.AddComponent<Animator>();
                }
            }
            else
            {
                // 查看List
                if (btn.GetComponent<Animator>().runtimeAnimatorController != null)
                {
                    List<string> data = new List<string>();
                    var list = btn.GetComponent<Animator>().runtimeAnimatorController.animationClips;
                    for (int i = 0; i < list.Length; i++)
                    {
                        data.Add(list[i].name);
                    }
                    chooseIndex = EditorGUILayout.Popup("点击播放的动画", chooseIndex, data.ToArray());
                    btn.animateClipName = list[chooseIndex].name;
                    EditorGUILayout.PropertyField(animationProp);
                    EditorGUILayout.LabelField("动画播放占比(0.1-1.0):");
                    animPrecent = EditorGUILayout.Slider(animPrecentProp.floatValue, 0.1f, 1f);
                    animPrecentProp.floatValue = animPrecent;
                }
            }
        }
        
        serializedObject.ApplyModifiedProperties();
    }
    
    [MenuItem("FMFramework/UI-Tools/Replace Button Component")]
    private static void UIScaler()
    {
        GameObject go = Selection.activeGameObject;
        Button btn = go.GetComponent<Button>();
        Image img = go.GetComponent<Image>();
        RectTransform trans = go.GetComponent<RectTransform>();
        if (btn != null && img != null)
        {
            GameObject newRoot = new GameObject(go.name);
            newRoot.layer = go.layer;
            newRoot.transform.SetParent(go.transform.parent,false);
            newRoot.transform.SetSiblingIndex(go.transform.GetSiblingIndex());
            RectTransform rootTrans = newRoot.AddComponent<RectTransform>();
            rootTrans.sizeDelta = trans.sizeDelta;
            rootTrans.position = trans.position;
            go.SetParent(newRoot);
            Object.DestroyImmediate(btn);
            img.raycastTarget = false;

            Image rootImg = newRoot.AddComponent<Image>();
            rootImg.raycastTarget = trans;
            rootImg.color = new Color(1,1,1,0);
            newRoot.GetComponent<CanvasRenderer>().cullTransparentMesh = true;

            var btnCom = newRoot.AddComponent<UGUIButtonEx>();
            btnCom.scaleTweemTarget = trans;
            btnCom.colorTweenTarget = img;
            btnCom.pressScale = UGUIEditorCfg.Instance.componentParam.buttonPressScaleOnReplace;
            Debug.Log("Replace UGUIButtonEx Success.");
        }
        else
        {
            Debug.LogError("Replace UGUIButtonEx Fail. <Button> or <Image> component Not Found!");
        }
    }
}
